<?php

/**
 * @category   Ac
 * @package    Application_Model
 * @copyright  Orient Network Ltd
 * @license    Closed
 * @author     John Wilson Lee
 */
class Model_Like extends Ac_Model
{
    protected static  $_collection = 'like';

    /**
     *
     * @var array define the requirements for this model
     */
    protected static $_requirements = array(
    );

    protected static $_transcript = array(
        '_id'	=> '_id',
        'uid'	=> 'uniqid',
        'us'	=> 'user',
        'obj'	=> 'object',
        'ob_t'	=> 'object_type',
        'c_dt'	=> 'created_at',
        'us_ob'	=> 'user_object',
   );

    public function __construct($data = array(), $config = array())
	{
        $config['collection'] = 'like';
        self::ensureIndex(array('obj'));
        self::ensureIndex(array('us'));
        self::ensureIndex(array('uid'));
        parent::__construct($data, $config);
    }

    public function __get($name)
    {
        if ($name == 'object') {
            return Model_Listing::find($this->obj);
        } elseif($name == 'user') {
            return Model_User::find($this->us);
        } else {
            return parent::__get($name);
        }
    }
    
    public static function like($id)
    {
		if(empty($id)){ 
			return false;
		}	

        $obj = Model_Listing::find($id);
        if (empty($obj)) {
            return false;
        }
		
		if (Zend_Registry::isRegistered('loggedin_user')) {
            $loggedin_user = Zend_Registry::get('loggedin_user')->_id;
            $h             = self::one(array('obj' => $obj->_id, 'user' => $loggedin_user));
        } else {
            //generate a unique id and put it in a cookie if it doesn 't exists
            if (empty($_COOKIE['ac_bookmark'])) {
                $uniqid = uniqid('', true);
                setcookie('ac_bookmark', $uniqid, time()+86400*365, "/");
                $_COOKIE['ac_bookmark'] = $uniqid;
            } else {
                $uniqid = $_COOKIE['ac_bookmark'];
            }
            $h = self::one(array('obj' => $obj->_id, 'uid'=> $uniqid));
        }
        $count = (int)self::getCount($id);
    	
        if (empty($h)) {
            $h = new self;
            $h->obj          = $obj->_id;
            $h->user_object  = $obj->us;
            if (!empty($loggedin_user)) {
                $h->user   = $loggedin_user;
            } else {
                $h->uniqid = $uniqid;
            }
            $obj->count_likes = $count + 1;
            $obj->save();
            $usr = Model_User::find($obj->us);
            if(!empty($usr)) {
            	if(!empty($usr->cid)){
            		$usr = $usr->company;
            	}
            	
                $usr->count_likes = intval($usr->count_likes) + 1;
                $usr->save();
            }
            $h->created_at = new MongoDate(time());
            $h->object_type = $obj->ca;
            $h->save();
		}
        $count = (int)self::getCount($id);
        return $count;

	}
	
	public static function isLiked($id)
	{
		if (empty($id)) { 
			return false;
		}	

		if (Zend_Registry::isRegistered('loggedin_user')) {
            $follower = Zend_Registry::get('loggedin_user')->_id;
            $h = self::one(array('obj' => $id, 'user' => $follower));
        } else {
            //generate a unique id and put it in a cookie if it doesn 't exists
            if (empty($_COOKIE['ac_bookmark'])) {
                $uniqid = uniqid('', true);
                setcookie('ac_bookmark', $uniqid, time()+86400*365, "/");
                $_COOKIE['ac_bookmark'] = $uniqid;
            } else {
                $uniqid = $_COOKIE['ac_bookmark'];
            }
            $h = self::one(array('obj' => $id, 'uid'=> $uniqid));
        }

        
        if (empty($h)) {
            return false;
        } else {
			return true;
		}
	}

    public static function getCount($id) {

        $obj = Model_Listing::find($id);

        if (!empty($obj) && !empty($obj->count_likes)) {
            return $obj->count_likes;
        }

        $res = self::all(array('obj'=> new MongoId($id)))->count();
        //update counter if we have to
        if (!empty($obj)) {
            if ($res != $obj->count_likes) {
                $obj->count_likes = $res;            
                $obj->save();
            }
        }
        return $res > 0 ? $res : '';
    }

    public static function getCountPerUser($id) {
        $res = self::all(array('us_ob'=> new MongoId($id)))->count(); 
        
        $user = Model_User::find($id);
        if(empty($user)){
            $user = Model_Company::find($id);
        }
        if(empty($user)){
            return false;
        }
        if (!empty($user->cid)) {
            $user= $user->company;
        } 
        //update counter if we have to
        if (!empty($user)) {
            if ($user->count_likes != $res) {
                $user->count_likes = $res;
                $user->save();
            }
        }
        return $res > 0 ? $res : '';
    }

	public static function unlike($id, $type = Model_Listing::TYPE)
	{
		if(empty($id)){ 
			return false;
		}	

        $obj = Model_Listing::find($id);

        if (empty($obj)) {
            return false;
        }
	
		if (Zend_Registry::isRegistered('loggedin_user')) {
            $follower = Zend_Registry::get('loggedin_user')->_id;
            $h = self::remove(array('obj' => $obj->_id, 'us' => $follower));
            $obj->count_likes = $obj->count_likes - 1;
            $obj->save();

            $usr = Model_User::find($obj->us);
            if(!empty($usr)) {
                $usr->count_likes = intval($usr->count_likes) - 1;
                $usr->save();
            }
            
            return self::getCount($id);
        } else {
            //generate a unique id and put it in a cookie if it doesn 't exists
            if (empty($_COOKIE['ac_bookmark'])) {
                $uniqid = uniqid('', true);
                setcookie('ac_bookmark', $uniqid, time()+86400*365, "/");
                $_COOKIE['ac_bookmark'] = $uniqid;
            } else {
                $uniqid = $_COOKIE['ac_bookmark'];
            }
            $h = self::remove(array('obj' => $obj->_id, 'uid'=> $uniqid));
            $obj->count_likes = $obj->count_likes - 1;
            $obj->save();
            $usr = Model_User::find($obj->us);
            if(!empty($usr)) {
                $usr->count_likes = intval($usr->count_likes) - 1;
                $usr->save();
            }

            return self::getCount($id);
        }

        return false;
	}
	
    
}
